﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.IO;
using HumanResourceSystem.Models;



namespace HumanResourceSystem.Controllers
{
    public class EmployeeNewsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: EmployeeNews
        [Authorize]
        public ActionResult Index(string EmployeeGenreDe, string EmployeeGenreSex, string SearchStringIDCard,string SearchStringName, string SearchStringAge)
        {
            var GenreLstDe = new List<string>();
            var GenreLstSex = new List<string>();

            var GenreQryDe = from e in db.EmployeeNews
                           orderby e.Department
                           select e.Department;

            var GenreQrySex = from e in db.EmployeeNews
                           orderby e.Sex
                           select e.Sex;

            GenreLstDe.AddRange(GenreQryDe.Distinct());
            GenreLstSex.AddRange(GenreQrySex.Distinct());
            ViewBag.EmployeeGenreDe = new SelectList(GenreLstDe);
            ViewBag.EmployeeGenreSex = new SelectList(GenreLstSex);

            var employee = from m in db.EmployeeNews
                         select m;

            if (!String.IsNullOrEmpty(SearchStringIDCard))
            {
                employee = employee.Where(s => s.IDCard.Contains(SearchStringIDCard));
            }

            if (!String.IsNullOrEmpty(SearchStringName))
            {
                employee = employee.Where(s => s.Name.Contains(SearchStringName));
            }

            if (!String.IsNullOrEmpty(SearchStringAge))
            {
                employee = employee.Where(s => (s.Age.ToString()).Contains(SearchStringAge));
            }

            if (!string.IsNullOrEmpty(EmployeeGenreDe))
            {
                employee = employee.Where(x => x.Department == EmployeeGenreDe);
            }
            if (!string.IsNullOrEmpty(EmployeeGenreSex))
            {
                employee = employee.Where(x => x.Sex == EmployeeGenreSex);
            }

            return View(employee);
        }

        // GET: EmployeeNews/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeNew employeeNew = db.EmployeeNews.Find(id);
            if (employeeNew == null)
            {
                return HttpNotFound();
            }
            return View(employeeNew);
        }

        // GET: EmployeeNews/Create
         
        public ActionResult Create()
        {
            return View();
        }

        // POST: EmployeeNews/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。

        [HttpPost]
        [ValidateAntiForgeryToken]
         public ActionResult Create([Bind(Include = "Id,IDCard,Department,Age,Name,Sex,Phone,Home,Edu,Tech,Work,Date,Path")] EmployeeNew employeeNew, HttpPostedFileBase file)
        {

            if (ModelState.IsValid)
            {
                var allowedUploadExtensions = new[] { ".jpg", ".bmp", ".png" };
            if (file == null ||
                file.ContentLength == 0 ||
                allowedUploadExtensions.Contains(Path.GetExtension(file.FileName)) == false)
            {
                return new HttpStatusCodeResult(400);
            }
            const String uploadPath = @"~/Uploads";
            var uploadServerPath = Server.MapPath(uploadPath);
            var randomFileName = String.Format("{0}{1}", Guid.NewGuid().ToString("D"), Path.GetExtension(file.FileName));

            if (Directory.Exists(uploadServerPath) == false)
            {
                Directory.CreateDirectory(uploadServerPath);
            }
                file.SaveAs(Path.Combine(uploadServerPath, randomFileName));
                employeeNew.Path = Path.Combine(uploadPath, randomFileName).ToString();
                db.EmployeeNews.Add(employeeNew);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(employeeNew);
        }

        // GET: EmployeeNews/Edit/5
         
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeNew employeeNew = db.EmployeeNews.Find(id);
            if (employeeNew == null)
            {
                return HttpNotFound();
            }
            return View(employeeNew);
        }

        // POST: EmployeeNews/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,IDCard,Department,Age,Name,Sex,Phone,Home,Edu,Tech,Work,Date,Path")] EmployeeNew employeeNew, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var allowedUploadExtensions = new[] { ".jpg", ".bmp", ".png" };
                if (file == null ||
                    file.ContentLength == 0 ||
                    allowedUploadExtensions.Contains(Path.GetExtension(file.FileName)) == false)
                {
                    return new HttpStatusCodeResult(400);
                }
                const String uploadPath = @"~/Uploads";
                var uploadServerPath = Server.MapPath(uploadPath);
                var randomFileName = String.Format("{0}{1}", Guid.NewGuid().ToString("D"), Path.GetExtension(file.FileName));

                if (Directory.Exists(uploadServerPath) == false)
                {
                    Directory.CreateDirectory(uploadServerPath);
                }
                file.SaveAs(Path.Combine(uploadServerPath, randomFileName));
                employeeNew.Path = Path.Combine(uploadPath, randomFileName).ToString();
                db.Entry(employeeNew).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(employeeNew);
        }

        // GET: EmployeeNews/Delete/5
        
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeNew employeeNew = db.EmployeeNews.Find(id);
            if (employeeNew == null)
            {
                return HttpNotFound();
            }
            return View(employeeNew);
        }

        // POST: EmployeeNews/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            EmployeeNew employeeNew = db.EmployeeNews.Find(id);
            db.EmployeeNews.Remove(employeeNew);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        
        public ActionResult Upload(HttpPostedFileBase file)
        {
            var allowedUploadExtensions = new[] { ".jpg", ".bmp", ".png" };
            if (file == null ||
                file.ContentLength == 0 ||
                allowedUploadExtensions.Contains(Path.GetExtension(file.FileName)) == false)
            {
                return new HttpStatusCodeResult(400);
            }
            const String uploadPath = @"~/Uploads";
            var uploadServerPath = Server.MapPath(uploadPath);
            var randomFileName = String.Format("{0}{1}", Guid.NewGuid().ToString("D"), Path.GetExtension(file.FileName));

            if (Directory.Exists(uploadServerPath) == false)
            {
                Directory.CreateDirectory(uploadServerPath);
            }
            file.SaveAs(Path.Combine(uploadServerPath, randomFileName));
            return Content("ok");

        }
       
    }
}
